package com.example.hao.musicplayer;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.hao.musicplayer.service.MusicService;

public class MainActivity extends AppCompatActivity implements ServiceConnection {

    private SeekBar seekBar;
    private TextView tv_progress;
    private MusicService.MyBind myBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        bindService(intent,this,Context.BIND_AUTO_CREATE);
    }

   public interface OnProgressCallBack{
        void onProgress(int progress);
    }

    private void initView() {
        seekBar = findViewById(R.id.seekBar);
        tv_progress = findViewById(R.id.tv_progress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                myBind.seekTo(seekBar.getProgress());
            }
        });
    }


    public void start(View view) {
        startService(new Intent(this, MusicService.class));
    }

    public void stop(View view) {
        stopService(new Intent(this, MusicService.class));
    }

    public void bind(View view) {
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent,this, Context.BIND_AUTO_CREATE);
    }

    public void unBind(View view) {
        unbindService(this);
    }

    public void play(View view) {
        myBind.playMusic();
    }

    public void pause(View view) {
        myBind.pause();
    }

    public void next(View view) {
        myBind.nextSong();
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        if (service instanceof MusicService.MyBind){
            myBind = (MusicService.MyBind) service;
            myBind.setProgressCallBack(new OnProgressCallBack() {
                @Override
                public void onProgress(final int progress) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            seekBar.setProgress(progress);
                            tv_progress.setText(progress+"%");

                        }
                    });
                }
            });
        }
    }
    private Handler handler = new Handler();
    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
